package com.bmw.newbabytimer;

import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dss on 14/12/24.
 */
public class ListCounterFragment extends CounterFragement{
    private GoodsAdapter goodsAdapter = null;
    private ListView counterView = null;

    public static ListCounterFragment newInstance() {
        ListCounterFragment fragment = new ListCounterFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_content, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        counterView = (ListView)view.findViewById(R.id.goods_list);
        counterView.setAdapter(getCounterViewAdapter());
        getLoaderManager().initLoader(0, null, this);
    }

    @Override
    public ViewGroup getCounterView() {
        return counterView;
    }

    @Override
    public GoodsAdapter getCounterViewAdapter() {
        if (null == goodsAdapter) {
            goodsAdapter = new ListAdatper(getActivity());
        }
        return goodsAdapter;
    }

    @Override
    public Loader<List<Goods>> onCreateLoader(int id, Bundle args) {
        return new ThrowableLoader<List<Goods>>(getActivity(), null) {
            @Override
            public List<Goods> loadData() throws Exception {
                return hock();
            }
        };
    }

    private List<Goods> hock() {
        List<Goods> goodslist = new ArrayList<Goods>();
        goodslist.add(getGoods1());
        goodslist.add(getGoods2());
        goodslist.add(getGoods1());
        goodslist.add(getGoods2());
        goodslist.add(getGoods1());
        goodslist.add(getGoods2());
        goodslist.add(getGoods1());
        goodslist.add(getGoods2());
        goodslist.add(getGoods1());
        goodslist.add(getGoods2());
        goodslist.add(getGoods1());
        goodslist.add(getGoods2());
        goodslist.add(getGoods1());
        goodslist.add(getGoods2());
        goodslist.add(getGoods1());
        goodslist.add(getGoods2());
        return goodslist;
    }

    private Goods getGoods1() {
        Goods goods = new Goods();
        goods.img = "http://imgs-qn.iliangcang.com/ware/topic/cover/1/83.jpg";
        return goods;
    }

    private Goods getGoods2() {
        Goods goods = new Goods();
        goods.img = "http://imgs-qn.iliangcang.com/ware/topic/cover/1/86.jpg";
        return goods;
    }
}
